<?php

class ArticleModel extends CoreModel {

	function __construct()
	{
		parent::__construct();
		$this->init();
	}

	public function init()
	{
		$this->loadModel('CategoryModel');
	}

	public function featured()
	{
		return $this->getPosts('showposts=3&cat='.CORE_CATEGORY_FEATURED.'&orderby=rand&order=ASC');
	}

	public function hostingReview()
	{
		return $this->getPosts('showposts=3&cat='.CORE_CATEGORY_HOSTING.'&orderby=date&order=ASC');
	}

	public function home()
	{
		return $this->getPosts('posts_per_page=15',true);
	}

	public function randomPosts()
	{
		return $this->getPosts('showposts=5&orderby=rand&order=ASC');
	}

	public function seriesPost($id) 
	{
		$terms_series = get_the_terms($id,'series');
		$title = "";
		$data = array();
		if ($terms_series) {
			$terms_series = reset($terms_series);
		}
		$count = $terms_series->count;
		if ($count > 1) {
			$args = array(
				'post_type' => 'post',
				'series' => $terms_series->slug,
				'posts_per_page' => -1,
				'order' => 'ASC',
				'orderby' => 'date',
				'post_status' => array('publish','future','pending'),
			);
			$getPosts =  query_posts($args);
			if(!empty($getPosts)){
				foreach($getPosts as $post) {
					setup_postdata($post);
					if($post->ID == $id) continue;
					$data[] = array(
						'id'=>$post->ID,
						'author'=>array('id'=>get_the_author_meta('ID'),'name'=>get_the_author_meta('display_name'),'link'=>get_author_posts_url(get_the_author_meta('ID'))),
						'date'=>date('d/m/Y',strtotime($post->post_date)),
						'title'=>$post->post_title,
						'content'=>apply_filters('the_content', $post->post_content),
						'excerpt'=>apply_filters('the_excerpt', get_the_excerpt()),
						'thumbnail'=>get_post_meta($post->ID, 'thumbnail', true),
						'status'=>$post->post_status,
						'slug'=>$post->post_name,
						'date_modified'=>date('d/m/Y',strtotime($post->post_modified)),
						'permalink'=>get_permalink($post->ID),
						'type'=>$post->post_type,
						'comments'=>$post->comment_count,
						'categories'=>$this->CategoryModel->getCategories($post->ID),
					);
				}
				wp_reset_query();
				return ['data'=>$data,'title'=>'bài viết cùng <span class="redColor">Series</span>'];
			}else{
				// return array();
				return ['data'=>$this->randomPosts(),'title'=>'bài viết ngẫu nhiên'];
			}
		}else{
			// return array();
			return ['data'=>$this->randomPosts(),'title'=>'bài viết ngẫu nhiên'];
		}
	}

	public function getPost($id)
	{
		$post = get_post($id);
		if(!empty($post)){
			$data = array(
				'id'=>$post->ID,
				'author'=>array('id'=>$post->post_author,'name'=>get_the_author_meta('display_name',$post->post_author),'link'=>get_author_posts_url($post->post_author),'description'=>get_the_author_meta('description',$post->post_author)),
				'date'=>$post->post_date,
				'title'=>$post->post_title,
				'excerpt'=>$this->getExcerpt($post->post_content),
				'content'=>apply_filters('the_content', $post->post_content),
				'thumbnail'=>get_post_meta($post->ID, 'thumbnail', true),
				'status'=>$post->post_status,
				'slug'=>$post->post_name,
				'date_modified'=>$post->post_modified,
				'permalink'=>get_permalink($post->ID),
				'type'=>$post->post_type,
				'comments'=>$post->comment_count,
				'categories'=>$this->CategoryModel->getCategories($post->ID),
			);
			return $data;
		}
	}

	public function getExcerpt($content)
	{
		$exPost = explode("<!--more-->",$content);
		return $exPost[0];
	}

	public function getPosts($query,$paged=false)
	{
		$data = array();
		$isPaged = ($paged == true) ? '&paged='.get_query_var('paged') : "";
		$getPosts = query_posts($query.$isPaged);
		if(!empty($getPosts)){
			foreach($getPosts as $post) {
				setup_postdata($post);
				$data[] = array(
					'id'=>$post->ID,
					'author'=>array('id'=>get_the_author_meta('ID'),'name'=>get_the_author_meta('display_name'),'link'=>get_author_posts_url(get_the_author_meta('ID')),'description'=>get_the_author_meta('description')),
					'date'=>date('d/m/Y',strtotime($post->post_date)),
					'title'=>$post->post_title,
					'content'=>apply_filters('the_content', $post->post_content),
					'excerpt'=>apply_filters('the_excerpt', get_the_excerpt()),
					'thumbnail'=>get_post_meta($post->ID, 'thumbnail', true),
					'status'=>$post->post_status,
					'slug'=>$post->post_name,
					'date_modified'=>date('d/m/Y',strtotime($post->post_modified)),
					'permalink'=>get_permalink($post->ID),
					'type'=>$post->post_type,
					'comments'=>$post->comment_count,
					'categories'=>$this->CategoryModel->getCategories($post->ID),
				);
			}
		}
		wp_reset_query();
		return $data;
	}

}